package uk.co.richardwkirk.parkrun.android.dataload;

import uk.co.richardwkirk.parkrun.data.ParkrunDataLoadException;
import uk.co.richardwkirk.parkrun.geo.GeoDataException;
import uk.co.richardwkirk.parkrun.geo.GeographicalData;
import uk.co.richardwkirk.parkrun.geo.Region;
import android.content.Context;
import android.util.Log;

public class WorldEventLoader extends ParkrunDataLoader {

	RegionDataLoadAdapter worldData = null;

	public WorldEventLoader(Context context) {
		super(context);
	}

	@Override
	protected void onStartLoading() {
		if (worldData != null) {
			deliverResult(worldData);
		} else {
			forceLoad();
		}
	}

	@Override
	public RegionDataLoadAdapter loadInBackground() {
		worldData = null;
		try {
			Log.v("PARKRUN", "Starting load of the parkrun world data.");
			Region world = GeographicalData.loadParkrunWorld();
			worldData = new RegionDataLoadAdapter(world);
			Log.v("PARKRUN", "Completed load of the parkrun world data.");
		} catch (ParkrunDataLoadException e) {
			Log.e("PARKRUN", "Could not load parkrun geo data.", e);
		} catch (GeoDataException e) {
			Log.e("PARKRUN", "Could not parse parkrun geo data.", e);
		}
		return worldData;
	}

}
